﻿using AzukiMap.Map;
using AzukiMap.MapSource;
using AzukiMap.Renderer;
using AzukiMap.Request;
using Geotools.Geometries;
using System.Xml.Linq;
using System.Collections.Generic;
using System.Windows;

namespace AzukiMap.Layer
{
    public class PostGisLayer : VectorLayer
    {
        PostGisSource _pgSource;
        PostGisVectorRequest _pgRequest;
        PostGisVectorParser _pgParser;

        public PostGisSource PgSource
        {
            get { return _pgSource; }
            set { 
                _pgSource = value;
                SetRequest(_pgSource);
            }
        }

        public PostGisLayer(PostGisSource pgSource , MapCanvas mapCanvas, string name, GTPolygon extent)
            : base((VectorSource)pgSource, mapCanvas, name, extent)
        {
            SetRequest(pgSource);
        }

        public PostGisLayer(PostGisSource pgSource, MapCanvas mapCanvas, string name)
            : base((VectorSource)pgSource, mapCanvas, name)
        {
            SetRequest(pgSource);
        }

        public PostGisLayer(MapCanvas mapCanvas)
            : base(new PostGisSource(), mapCanvas, "")
        {
        }

        void SetRequest(PostGisSource pgSource)
        {
            _pgSource = pgSource;
            if (pgSource.GetColumnDataUri != null)
            {
                _pgRequest = new PostGisVectorRequest(_mapCanvas, this, pgSource.GetColumnDataUri);
            }
            else
            {
                _pgRequest = new PostGisVectorRequest(_mapCanvas, this);
            }
            _pgRequest.GetDataCompleted += new GetDataCompletedHandler(_pgRequest_GetDataCompleted);
        }

        public override void Draw()
        {
            if (_isRendering)
            {
                return;
            }
            if (!_isVisible)
            {
                return;
            }
            if (!CheckScale())
            {
                return;
            }
            _isRendering = true;
            _pgRequest.Request();
        }

        void _pgRequest_GetDataCompleted(object sender, GetDataCompletedArgs e)
        {
            _pgParser = new PostGisVectorParser(e.DataStream);
            int numOfGeom = _pgParser.NumOfObjects;

            // TODO: get uniq ID from records.
            int id = 0;
            IVectorStyle vStyle;
            foreach (PostGisRecord record in _pgParser)
            {
                // No layer theme
                if (record.ThemeId == -1)
                {
                    vStyle = _defaultStyle;
                }
                else
                {
                    vStyle = _layerThemes[record.ThemeId].Style;
                }
                List<FrameworkElement> elems = _renderer.DrawGeometry(id, record.Geometry, vStyle);
                SetEventsOnElement(elems);
            }
            _isRendering = false;
        }

        public override XElement ToXml()
        {
            return ToXml("PostGisLayer");
        }

        public override XElement ToXml(string elementName)
        {
            XElement vectorLayerXml = base.ToXml(elementName);
            return vectorLayerXml;
        }

        public override void Parse(XElement layerXml)
        {
            base.Parse(layerXml);
            PostGisSource source = new PostGisSource();
            source.Parse(layerXml.Element("PostGisSource"));
            _pgSource = source;
            _data = source;
            _mapSource = source;
            _vectorType = source.VectorType;
            SetRequest(_pgSource);
        }
    }
}
